package com.gamecauca.screen;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.gamecauca.Assets;
import com.gamecauca.Input;
import com.gamecauca.ItemDetails;
import com.gamecauca.common.Constant;
import com.gamecauca.common.OverlapTester;
import com.gamecauca.item.ItemPos;


public class MarketScreen extends Screen{

	OrthographicCamera camera;
	Vector3 touchPoint;
	SpriteBatch batch;
	
	Rectangle itemBound;
	Rectangle userItemBound;
	Rectangle itemBoxBound;
	Rectangle item1Bound;
	Rectangle item2Bound;
	
	Rectangle buyBound;
	Rectangle sellBound;
	
	int itemStt;
	int userItemStt;
	int userBoxStt;
	
	ItemDetails itemTemp;
	boolean itemEnable;
	int countQuantity;
	int countUserItem;
	
	int moneyBuy;
	int moneySell;
	float x1, y1;
	float x2, y2;
	int itemId;
	boolean flgSell = false;
	
	//int money = 10000;
	
	
	float MARKETPADDINGY = (192 + (Assets.PADDING*2)) + (Constant.SCREEN_HEIGHT - (192 + (Assets.PADDING*2)))/2;
	
	boolean upHighlight = false;
	boolean downHighlight = false;
	boolean buyHighlight = false;
	boolean sellHighlight = false;
	
	float xBox = Assets.firstPADDING + 6*(Assets.PADDING + 64) + 128 + Assets.PADDING + 10;
	float yBox;
	
	float xItem, yItem;
	
	boolean flgDrag = false;
	boolean dragOK = false;
	
	boolean flgDragBox = false;
	boolean dragOKBox = false;
	
	ItemPos[] userItemPos = {new ItemPos(Assets.firstPADDING + 3*(Assets.PADDING + 64) + 128 + Assets.PADDING + 11 , (MARKETPADDINGY - 64)),
							 new ItemPos(Assets.firstPADDING + 4*(Assets.PADDING + 64) + 128 + Assets.PADDING + 2 , (MARKETPADDINGY - 64)),
							 new ItemPos(Assets.firstPADDING + 5*(Assets.PADDING + 64) + 128 + Assets.PADDING - 6 , (MARKETPADDINGY - 64)),
							 new ItemPos(Assets.firstPADDING + 3*(Assets.PADDING + 64) + 128 + Assets.PADDING + 11, (MARKETPADDINGY - 64) - 1*(Assets.PADDING + 64)),
							 new ItemPos(Assets.firstPADDING + 4*(Assets.PADDING + 64) + 128 + Assets.PADDING + 2, (MARKETPADDINGY - 64) - 1*(Assets.PADDING + 64)),
							 new ItemPos(Assets.firstPADDING + 5*(Assets.PADDING + 64) + 128 + Assets.PADDING - 6, (MARKETPADDINGY - 64) - 1*(Assets.PADDING + 64)),
							 new ItemPos(Assets.firstPADDING + 3*(Assets.PADDING + 64) + 128 + Assets.PADDING + 11, (MARKETPADDINGY - 64) - 2*(Assets.PADDING + 64)),
							 new ItemPos(Assets.firstPADDING + 4*(Assets.PADDING + 64) + 128 + Assets.PADDING + 2, (MARKETPADDINGY - 64) - 2*(Assets.PADDING + 64)),
							 new ItemPos(Assets.firstPADDING + 5*(Assets.PADDING + 64) + 128 + Assets.PADDING - 6, (MARKETPADDINGY - 64) - 2*(Assets.PADDING + 64))};
	
	Screen parent;
	
	public MarketScreen(Game game, Screen parent) {
		super(game);
		// TODO Auto-generated constructor stub
		this.parent = parent;
		
		camera = new OrthographicCamera(Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
		camera.position.set(Constant.SCREEN_WIDTH/2, Constant.SCREEN_HEIGHT/2, 0);
		touchPoint = new Vector3();
		batch = new SpriteBatch();
		
		itemBound = new Rectangle();
		userItemBound = new Rectangle();
		itemBoxBound = new Rectangle();
		
		item1Bound = new Rectangle(Assets.firstPADDING + ((192 + (Assets.PADDING*2))/2 - 32), MARKETPADDINGY + Assets.PADDING, 64, 64);
		item2Bound = new Rectangle(Assets.firstPADDING + ((192 + (Assets.PADDING*2))/2 - 32), (Constant.SCREEN_HEIGHT - (192 + (Assets.PADDING*2)))/2 - Assets.PADDING - 32, 64, 64);
		
		buyBound = new Rectangle(Assets.firstPADDING + 3*(Assets.PADDING + 64) - 16, (MARKETPADDINGY - 64 + 8), 128 + 32, 32 + 32);
		sellBound = new Rectangle(Assets.firstPADDING + 3*(Assets.PADDING + 64) - 5, (MARKETPADDINGY - 64) - 2*(Assets.PADDING + 64) - 8, 128 + 20, 32 + 32);
	}
	
	public void tick(Input input){
		//System.out.println("start method tick(Input input) + MarketScreen class");
		input.buttons[Input.OPENDMARKET] = false;
	}

	@Override
	public void present() {
		// TODO Auto-generated method stub
//		GLCommon gl = Gdx.gl;
//		gl.glEnable(GL10.GL_TEXTURE_2D);
//		gl.glClearColor(1, 0, 0, 1);
//		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		camera.update();
		batch.setProjectionMatrix(camera.combined);
		batch.enableBlending();
		batch.begin();
		
		batch.draw(upHighlight ? Assets.ITEM_TYPE_1[1][0] : Assets.ITEM_TYPE_1[0][0], Assets.firstPADDING + ((192 + (Assets.PADDING*2))/2 - 32), MARKETPADDINGY + Assets.PADDING);
		batch.draw(downHighlight ? Assets.ITEM_TYPE_2[1][0] : Assets.ITEM_TYPE_2[0][0], Assets.firstPADDING + ((192 + (Assets.PADDING*2))/2 - 32), (Constant.SCREEN_HEIGHT - (192 + (Assets.PADDING*2)))/2 - Assets.PADDING - 32);
		
		batch.draw(Assets.marketBackgroundRegion, 0, (Constant.SCREEN_HEIGHT - 290)/2, 210, 145, 415, 290, 1, 1, 180);
		batch.draw(Assets.marketBackground, 256, (Constant.SCREEN_HEIGHT - 290 + 6)/2, 415, 290);	
		
		drawItems(Assets.itemSelection);
		
		//batch.draw(Assets.buy[0][0],Assets.firstPADDING + 3*(Assets.PADDING + 64) - 16, (MARKETPADDINGY - 64 + 8), 128 + 32, 32 + 32);
		batch.draw(buyHighlight ? Assets.buy[1][0] : Assets.buy[0][0], Assets.firstPADDING + 3*(Assets.PADDING + 64) + 32, (MARKETPADDINGY - 64 + 32) - 8);
		batch.draw(Assets.gold, Assets.firstPADDING + 3*(Assets.PADDING + 64) + 128 - 16 - 16, (MARKETPADDINGY - 64 + 32) - Assets.PADDING - 32);
		
		batch.draw(Assets.marketImg, Assets.firstPADDING + 3*(Assets.PADDING + 64) + 8, (MARKETPADDINGY - 64) - (Assets.PADDING + 32) - 16);
		batch.draw(Assets.boxImg, Assets.firstPADDING + 3*(Assets.PADDING + 64) + 64 + 28, (MARKETPADDINGY - 64) - (Assets.PADDING + 32) - 16);
		batch.draw(Assets.marketImg, xBox, (MARKETPADDINGY - 64) + 1*(Assets.PADDING + 64) + 10, 64, 64);
		
		batch.draw(Assets.gold, Assets.firstPADDING + 3*(Assets.PADDING + 64) + 128 - 16 - 16, (MARKETPADDINGY - 64) - 2*(Assets.PADDING + 32) - 26 + 16);
		batch.draw(sellHighlight ? Assets.sell[1][0] : Assets.sell[0][0], Assets.firstPADDING + 3*(Assets.PADDING + 64) + 38, (MARKETPADDINGY - 64) - 2*(Assets.PADDING + 64) + 8);
		//batch.draw(Assets.sell[0][0], Assets.firstPADDING + 3*(Assets.PADDING + 64) - 5, (MARKETPADDINGY - 64) - 2*(Assets.PADDING + 64) - 8, 128 + 20, 32 + 32);
		if(moneyBuy > 0)
		{
			float moneyTradeHeight = Assets.font.getBounds(moneyBuy + "").height;
			float moneyTradeWidth = Assets.font.getBounds(moneyBuy + "").width;
			Assets.font.draw(batch, moneyBuy + "", Assets.firstPADDING + 3*(Assets.PADDING + 64) + (Assets.PADDING + 64) - moneyTradeWidth + 16,
															(MARKETPADDINGY - 64 + 32) - Assets.PADDING + moneyTradeHeight - 24);
		}
		
		if(moneySell > 0)
		{
			float moneyTradeHeight = Assets.font.getBounds(moneySell + "").height;
			float moneyTradeWidth = Assets.font.getBounds(moneySell + "").width;
			Assets.font.draw(batch, moneySell + "", Assets.firstPADDING + 3*(Assets.PADDING + 64) + (Assets.PADDING + 64) - moneyTradeWidth + 16, 
							(MARKETPADDINGY - 64) - 2*(Assets.PADDING + 32) - 26 + moneyTradeHeight + 24);
		}
		
		//drawContainer();
		
		float moneyHeight = Assets.font.getBounds(GameScreen.totalMoney + "$").height;
		Assets.font.draw(batch, GameScreen.totalMoney + "$", Constant.SCREEN_WIDTH - 250, Constant.SCREEN_HEIGHT - moneyHeight + 5);
		
		drawItemBoxContainer();
		drawUserItems();
		drawItemBox();
		
		
		batch.end();
		
		dragItem();
		dragItemBox();
		
		renderItemHandler();
			
	}
	
	public void renderItemHandler()
	{
		//System.out.println("start renderItemHandler() in MarketScreen class");
		if (Gdx.input.justTouched()) {
			
			camera.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
			
			if(OverlapTester.pointInRectangle(item1Bound, touchPoint.x, touchPoint.y))
			{
				upHighlight = true;
				downHighlight = false;
				
				if(Assets.itemSelection > 1)
					Assets.itemSelection--;
				else
					Assets.itemSelection = 1;
			}
			
			if(OverlapTester.pointInRectangle(item2Bound, touchPoint.x, touchPoint.y))
			{
				upHighlight = false;
				downHighlight = true;
				
				if(Assets.itemSelection < 4)
					Assets.itemSelection++;
				else
					Assets.itemSelection = 4;
				
			}
		
			int start = 0;
			int size = 1;
			
			if(Assets.itemSelection == 1)
			{
				start = 0;
				size = 9;
			}
			else if(Assets.itemSelection == 2)
			{
				start = 9;
				size = 18;
			}
			else if(Assets.itemSelection == 3){
				start = 18;
				size = 27;
			}
			else if(Assets.itemSelection == 4){
				start = 27;
				size = 29;
			}
			
			for(int i = start; i < size; i++)
			{
				itemBound.set(Assets.itemList.get(i).getX(), Assets.itemList.get(i).getY(), 64, 64);
				
				if(OverlapTester.pointInRectangle(itemBound, touchPoint.x, touchPoint.y))
				{
					moneyBuy = Assets.itemList.get(i).getPrice();
					itemStt = Assets.itemList.get(i).getId();
					
					if((/*Assets.itemList.get(i).getQuantity() > 0 && */GameScreen.totalMoney >= Assets.itemList.get(i).getPrice() && Assets.userItems.size() < 9) 
							|| (checkItemId(Assets.itemList.get(i).getId(), 0) /*&& Assets.itemList.get(i).getQuantity() > 0*/ 
									&& GameScreen.totalMoney >= Assets.itemList.get(i).getPrice()))
						buyHighlight = true;
					
				}
			}
			
			if((moneyBuy > 0 && Assets.userItems.size() < 9) || (moneyBuy > 0 && GameScreen.totalMoney > 0 && checkItemId(itemStt, 0)))
			{
				if(OverlapTester.pointInRectangle(buyBound, touchPoint.x, touchPoint.y))
				{
					buyHighlight = false;
//					System.out.println("Stt: " + itemStt);
					for(int i = 0; i < Assets.itemList.size(); i++)
					{
						if(Assets.itemList.get(i).getId() == itemStt)
						{
							if(GameScreen.totalMoney < Assets.itemList.get(i).getPrice())
							{
								moneyBuy = 0;
								break;
							}
							else
							{
								itemTemp = Assets.itemList.get(i);
								
								for(int j = 0; j < Assets.userItems.size(); j++)
								{
									if(Assets.userItems.get(j).getId() == itemStt)
									{
										if(Assets.itemList.get(i).getQuantity() > 0)
										{
											Assets.userItems.get(j).setQuantity(Assets.userItems.get(j).getQuantity() + 1);
										}
										
										itemEnable = true;
										break;
									}
									else
									{
										itemEnable = false;
									}
								}
								
//								System.out.println("itemEnable: " + itemEnable);
								if(!itemEnable)
								{
									Assets.userItems.add(new ItemDetails(itemTemp.getId(), itemTemp.getPrice(),
											itemTemp.getName(), itemTemp.getInfo(), 1, itemTemp.getKind(), itemTemp.getIndex(), Assets.firstPADDING + (countUserItem%3 + 3)*(Assets.PADDING + 64) + Assets.PADDING + 128, 
											(MARKETPADDINGY - 64) - (countUserItem/3)*(Assets.PADDING + 64)));
									
									countUserItem++;
								}				
//								System.out.println("Size: " + Assets.userItems.size());
//								if(Assets.itemList.get(i).getQuantity() > 0)
//								{
//									Assets.itemList.get(i).setQuantity(Assets.itemList.get(i).getQuantity() - 1);
									GameScreen.totalMoney -= itemTemp.getPrice();
//								}
								
								moneyBuy = 0;
								
								break;
							}
							
						}					
					}
				}
			}
			
			// sell
			
			for(int i = 0; i < Assets.userItems.size(); i++)
			{
				userItemBound.set(Assets.userItems.get(i).getX(), Assets.userItems.get(i).getY(), 64, 64);
				if(OverlapTester.pointInRectangle(userItemBound, touchPoint.x, touchPoint.y))
				{
					moneySell = Assets.userItems.get(i).getPrice();
					userItemStt = Assets.userItems.get(i).getId();
					flgDrag = true;
					sellHighlight = true;
				}
			}
			
			for(int i = 0; i < Assets.userBox.size(); i++)
			{
				itemBoxBound.set(Assets.userBox.get(i).getX(), Assets.userBox.get(i).getY(), 64, 64);
				if(OverlapTester.pointInRectangle(itemBoxBound, touchPoint.x, touchPoint.y))
				{
					userBoxStt = Assets.userBox.get(i).getId();
					flgDragBox = true;
				}
			}
			
			
			if(moneySell > 0)
			{
				if(OverlapTester.pointInRectangle(sellBound, touchPoint.x, touchPoint.y))
				{
					flgSell = false;
					sellHighlight = false;
					moneySell = 0;
					
					for(int i = 0; i < Assets.userItems.size(); i++)
					{
						
						if(Assets.userItems.get(i).getId() == userItemStt)
						{
							GameScreen.totalMoney += Assets.userItems.get(i).getPrice();
							x1 = Assets.userItems.get(i).getX();
							y1 = Assets.userItems.get(i).getY();
							itemId = i;
							
							if(Assets.userItems.get(i).getQuantity() > 1)
							{
								Assets.userItems.get(i).setQuantity(Assets.userItems.get(i).getQuantity() - 1);
								break;
							}
							
							flgSell = true;
											
						}
						
						if(flgSell)
						{
							x2 = Assets.userItems.get(i).getX();
							y2 = Assets.userItems.get(i).getY();
							
							Assets.userItems.get(i).setX(x1);
							Assets.userItems.get(i).setY(y1);
							
							x1 = x2;
							y1 = y2;
						}
								
						if(i == Assets.userItems.size() - 1)
						{
							Assets.userItems.remove(itemId);
							itemEnable = false;
							countUserItem -= 1;
						}
					
					}
					
//					for(int j = 0; j < Assets.itemList.size(); j++)
//					{
//						if(Assets.itemList.get(j).getId() == userItemStt)
//						{
//							
//							Assets.itemList.get(j).setQuantity(Assets.itemList.get(j).getQuantity() + 1);
//							break;
//						}
//					}
				}
			}
			
			if((OverlapTester.pointInRectangle(xBox, (MARKETPADDINGY - 64) + 1*(Assets.PADDING + 64) + 10, 64, 64, touchPoint.x, touchPoint.y)))
			{
			
				this.game.setScreen(parent);	
				return;
			}
			
			
		}
	}
	
	public void drawItems(int select)
	{
		System.out.println("start method drawItems(int select) in MarketScreeen class");
		int start = 0;
		int size = 1;
		
		if(select == 1)
		{
			start = 0;
			size = 9;
		}
		else if(select == 2)
		{
			start = 9;
			size = 18;
		}
		else if(select == 3)
		{
			start = 18;
			size = 27;
		}
		else if(select == 4){
			start = 27;
			size = 29;
		}
		
		for(int i = start; i < size; i++)
		{
			batch.draw(Assets.itemImgs[i], Assets.itemList.get(i).getX(), Assets.itemList.get(i).getY(), 60, 60);
//			float quantityHeight = Assets.font.getBounds("x" + Assets.itemList.get(i).getQuantity()).height;
//			float quantityWidth = Assets.font.getBounds("x" + Assets.itemList.get(i).getQuantity()).width;
//			
//			Assets.font.draw(batch, "x" + Assets.itemList.get(i).getQuantity(), 
//							Assets.itemList.get(i).getX() + ((64 - quantityWidth)/2), 
//							Assets.itemList.get(i).getY() + ((64 + quantityHeight)/2));
		}
	
	}
	
	public void drawUserItems()
	{
		int x = 0;
		int y = 0;
		int column = 3;
		int row = 0;
		
		int id = -1;
		
		for(int i = 0; i < Assets.userItems.size(); i++)
		{
//			System.out.println("======================= Assets.userItems.size()  ==============="+Assets.userItems.size());
			if(i == 0)
			{
				x = Assets.firstPADDING + column * (Assets.PADDING + 64) + Assets.PADDING + 128 + 11;
				y = (int)(MARKETPADDINGY - 64);
				
				column++;
				row = 1;
			}
			else
			{
				if(i % 3 == 0)
				{
					x = Assets.firstPADDING + column * (Assets.PADDING + 64) + Assets.PADDING + 128 + 11;
					y = (int)(MARKETPADDINGY - 64) - row * (Assets.PADDING + 64);
					
					column++;
        			row++;
				}
				else
				{
					x = Assets.firstPADDING + column * (Assets.PADDING + 64) + Assets.PADDING + 128 + 2;
					y = (int)(MARKETPADDINGY - 64) - ((row - 1) * (Assets.PADDING + 64));
					
					if(column % 5 == 0)
    				{
						x = Assets.firstPADDING + column * (Assets.PADDING + 64) + Assets.PADDING + 128 - 6;
    					column = 2;
    				}
    				
    				column++;
				}
			}
			
			Assets.userItems.get(i).setX(x);
			Assets.userItems.get(i).setY(y);
			
			
			if(Assets.userItems.get(i).getId() != userItemStt)
			{
				batch.draw(Assets.itemImgs[Assets.userItems.get(i).getId()], x, y);
				float quantityHeight = Assets.font.getBounds("x" + Assets.userItems.get(i).getQuantity()).height;
				float quantityWidth = Assets.font.getBounds("x" + Assets.userItems.get(i).getQuantity()).width;
				
				Assets.font.draw(batch, "x" + Assets.userItems.get(i).getQuantity(), 
									x + ((64 - quantityWidth)/2), 
									y + ((64 + quantityHeight)/2));
			}
			else
			{
				if(!flgDrag)
				{
					if(dragOK)
					{		
						if(!checkBoxId(Assets.userItems.get(i).getId(), Assets.userItems.get(i).getQuantity()))
						{
							
							Assets.userBox.add(new ItemDetails(Assets.userItems.get(i).getId(),
									   Assets.userItems.get(i).getPrice(),
									   Assets.userItems.get(i).getName(),
									   Assets.userItems.get(i).getInfo(),
									   Assets.userItems.get(i).getQuantity(),
									   Assets.userItems.get(i).getKind(),
									   Assets.userItems.get(i).getIndex(),
									   xBox, yBox));
						}
						
						sellHighlight = false;
						
						itemEnable = false;
						id = i;
						dragOK = false;
						moneySell = 0;
					}
					else
					{
						batch.draw(Assets.itemImgs[Assets.userItems.get(i).getId()], x, y);
						float quantityHeight = Assets.font.getBounds("x" + Assets.userItems.get(i).getQuantity()).height;
						float quantityWidth = Assets.font.getBounds("x" + Assets.userItems.get(i).getQuantity()).width;
							
						Assets.font.draw(batch, "x" + Assets.userItems.get(i).getQuantity(), 
											x + ((64 - quantityWidth)/2), 
											y + ((64 + quantityHeight)/2));
					}
					
				}
			}
			
			if(i == Assets.userItems.size() - 1)
			{
				if(id != -1)
				{
					Assets.userItems.remove(id);
				}
			}
		}
	}
	
	public void drawContainer()
	{
		//System.out.println("start method drawContainer() in MarketScreen class");
		batch.draw(Assets.container, Assets.firstPADDING + 3*(Assets.PADDING + 64) + 128 + Assets.PADDING , (MARKETPADDINGY - 64));
		batch.draw(Assets.container, Assets.firstPADDING + 4*(Assets.PADDING + 64) + 128 + Assets.PADDING, (MARKETPADDINGY - 64));
		batch.draw(Assets.container, Assets.firstPADDING + 5*(Assets.PADDING + 64) + 128 + Assets.PADDING, (MARKETPADDINGY - 64));
		
		batch.draw(Assets.container, Assets.firstPADDING + 3*(Assets.PADDING + 64) + 128 + Assets.PADDING, (MARKETPADDINGY - 64) - 1*(Assets.PADDING + 64));
		batch.draw(Assets.container, Assets.firstPADDING + 4*(Assets.PADDING + 64) + 128 + Assets.PADDING, (MARKETPADDINGY - 64) - 1*(Assets.PADDING + 64));
		batch.draw(Assets.container, Assets.firstPADDING + 5*(Assets.PADDING + 64) + 128 + Assets.PADDING, (MARKETPADDINGY - 64) - 1*(Assets.PADDING + 64));
		
		batch.draw(Assets.container, Assets.firstPADDING + 3*(Assets.PADDING + 64) + 128 + Assets.PADDING, (MARKETPADDINGY - 64) - 2*(Assets.PADDING + 64));
		batch.draw(Assets.container, Assets.firstPADDING + 4*(Assets.PADDING + 64) + 128 + Assets.PADDING, (MARKETPADDINGY - 64) - 2*(Assets.PADDING + 64));
		batch.draw(Assets.container, Assets.firstPADDING + 5*(Assets.PADDING + 64) + 128 + Assets.PADDING, (MARKETPADDINGY - 64) - 2*(Assets.PADDING + 64));
	}
	
	public void drawItemBoxContainer()
	{
		//batch.draw(Assets.container, Assets.firstPADDING + 6*(Assets.PADDING + 64) + 128 + Assets.PADDING + 10, (MARKETPADDINGY - 64) + 1*(Assets.PADDING + 64) + 10);
		batch.draw(Assets.container, Assets.firstPADDING + 6*(Assets.PADDING + 64) + 128 + Assets.PADDING + 10, (MARKETPADDINGY - 64) + 10);
		batch.draw(Assets.container, Assets.firstPADDING + 6*(Assets.PADDING + 64) + 128 + Assets.PADDING + 10, (MARKETPADDINGY - 64) - 1*(Assets.PADDING + 64) + 10);
		
		batch.draw(Assets.container, Assets.firstPADDING + 6*(Assets.PADDING + 64) + 128 + Assets.PADDING + 10, (MARKETPADDINGY - 64) - 2*(Assets.PADDING + 64) + 10);
		batch.draw(Assets.container, Assets.firstPADDING + 6*(Assets.PADDING + 64) + 128 + Assets.PADDING + 10, (MARKETPADDINGY - 64) - 3*(Assets.PADDING + 64) + 10);
	}

	
	public boolean checkContainBox(float y)
	{
		for(int i = 0; i < Assets.userBox.size(); i++)
		{
			if(Assets.userBox.get(i).getY() == y)
			{
				return true;
			}
		}
		
		return false;
	}
	
	public boolean checkBoxId(int id, int quantity)
	{
		
		for(int i = 0; i < Assets.userBox.size(); i++)
		{
			if(Assets.userBox.get(i).getId() == id)
			{
				Assets.userBox.get(i).setQuantity(Assets.userBox.get(i).getQuantity() + quantity);
				return true;
			}
			else
			{
				if(i == Assets.userBox.size() - 1 && Assets.userBox.size() == 4)
				{
					return false;
				}
			}
		}
		
		return false;
	}
	
	public boolean checkContainId(float x, float y)
	{
		for(int i = 0; i < Assets.userItems.size(); i++)
		{
			if(Assets.userItems.get(i).getX() == x && Assets.userItems.get(i).getY() == y)
			{
				return true;
			}
		}
		
		return false;
	}
	
	public boolean checkItemId(int id, int quantity)
	{
		for(int i = 0; i < Assets.userItems.size(); i++)
		{
			if(Assets.userItems.get(i).getId() == id)
			{
				Assets.userItems.get(i).setQuantity(Assets.userItems.get(i).getQuantity() + quantity);
				return true;
			}
			else
			{
				if(i == Assets.userBox.size() - 1 && Assets.userBox.size() == 9)
				{
					return false;
				}
			}
		}
		
		
		return false;
	}
	
	public void dragItem()
	{
		//System.out.println("start method dragItem() in MarketScreen class");
		if(flgDrag)
		{
			batch.begin();
			
			float x = Gdx.input.getX(0) + 120;
			float y = Gdx.graphics.getHeight() - Gdx.input.getY(0) + 64;
			
			if (Gdx.input.isTouched(0) == false)
			{
				flgDrag = false;
				
					//if((OverlapTester.pointInRectangle(xBox - 40, (MARKETPADDINGY - 64) + 1*(Assets.PADDING + 64) + 10 - 20, 64, 64, x + 32, y + 32)
					//	&& !checkContainBox((MARKETPADDINGY - 64) + 1*(Assets.PADDING + 64) + 10)) || 
					//	(OverlapTester.pointInRectangle(xBox - 40, (MARKETPADDINGY - 64) + 1*(Assets.PADDING + 64) + 10 - 20, 64, 64, x + 32, y + 32)
					//			&& checkBoxId(userItemStt, 0)))
					//{
						
					//		yBox = (MARKETPADDINGY - 64) + 1*(Assets.PADDING + 64) + 10;
					//		dragOK = true;
					//}
					
					if((OverlapTester.pointInRectangle(xBox - 40, (MARKETPADDINGY - 64) + 10 - 20, 64, 64, x + 32, y + 32)
							&& !checkContainBox((MARKETPADDINGY - 64) + 10))|| 
					   (OverlapTester.pointInRectangle(xBox - 40, (MARKETPADDINGY - 64) + 10 - 20, 64, 64, x + 32, y + 32)
								&& checkBoxId(userItemStt, 0)))
					{
							yBox = (MARKETPADDINGY - 64) + 10;
							dragOK = true;
					}
					
					if((OverlapTester.pointInRectangle(xBox - 40, (MARKETPADDINGY - 64) - 1*(Assets.PADDING + 64) + 10 - 20, 64, 64, x + 32, y + 32)
							&& !checkContainBox((MARKETPADDINGY - 64) - 1*(Assets.PADDING + 64) + 10)) ||
						(OverlapTester.pointInRectangle(xBox - 40, (MARKETPADDINGY - 64) - 1*(Assets.PADDING + 64) + 10 - 20, 64, 64, x + 32, y + 32)
							&& checkBoxId(userItemStt, 0)))
					{
							yBox = (MARKETPADDINGY - 64) - 1*(Assets.PADDING + 64) + 10;
							dragOK = true;
					}
					
					if((OverlapTester.pointInRectangle(xBox - 40, (MARKETPADDINGY - 64) - 2*(Assets.PADDING + 64) + 30, 64, 64, x + 32, y + 32)
							&& !checkContainBox((MARKETPADDINGY - 64) - 2*(Assets.PADDING + 64) + 10)) ||
						(OverlapTester.pointInRectangle(xBox - 40, (MARKETPADDINGY - 64) - 2*(Assets.PADDING + 64) + 30, 64, 64, x + 32, y + 32)
							&& checkBoxId(userItemStt, 0)))
					{
							yBox = (MARKETPADDINGY - 64) - 2*(Assets.PADDING + 64) + 10;
							dragOK = true;
					}
					
					if((OverlapTester.pointInRectangle(xBox - 40, (MARKETPADDINGY - 64) - 3*(Assets.PADDING + 64) + 30, 64, 64, x + 32, y + 32)
							&& !checkContainBox((MARKETPADDINGY - 64) - 3*(Assets.PADDING + 64) + 10)) ||
						(OverlapTester.pointInRectangle(xBox - 40, (MARKETPADDINGY - 64) - 3*(Assets.PADDING + 64) + 30, 64, 64, x + 32, y + 32)
							&& checkBoxId(userItemStt, 0)))
					{
							yBox = (MARKETPADDINGY - 64) - 3*(Assets.PADDING + 64) + 10;
							dragOK = true;
					}
			
			}
			
			if(flgDrag)			
			{
				for(int i = 0; i < Assets.userItems.size(); i++)
				{
					if(Assets.userItems.get(i).getId() == userItemStt)
					{
						batch.draw(Assets.itemImgs[Assets.userItems.get(i).getId()], x, y);
						break;
					}
				}
			}
				
								
			batch.end();
		}
	}
	
	public void drawItemBox()
	{
		//System.out.println("start method drawItemBox() in Market class");
		float y = 0;
		int row = 0;
		int id = -1;
		
		for(int i = 0; i < Assets.userBox.size(); i++)
		{
			y = (MARKETPADDINGY - 64) - row*(Assets.PADDING + 64) + 10;		
			row++;
			
			Assets.userBox.get(i).setY(y);
			
			if(Assets.userBox.get(i).getId() != userBoxStt)
			{
				batch.draw(Assets.itemImgs[Assets.userBox.get(i).getId()], Assets.userBox.get(i).getX(), Assets.userBox.get(i).getY());
				float quantityHeight = Assets.font.getBounds("x" + Assets.userBox.get(i).getQuantity()).height;
				float quantityWidth = Assets.font.getBounds("x" + Assets.userBox.get(i).getQuantity()).width;
					
				Assets.font.draw(batch, "x" + Assets.userBox.get(i).getQuantity(), 
									Assets.userBox.get(i).getX() + ((64 - quantityWidth)/2), 
									Assets.userBox.get(i).getY() + ((64 + quantityHeight)/2));
			}
			else
			{
				if(!flgDragBox)
				{
					if(dragOKBox)
					{
						if(!checkItemId(Assets.userBox.get(i).getId(), Assets.userBox.get(i).getQuantity()))
						{
							Assets.userItems.add(new ItemDetails(Assets.userBox.get(i).getId(), Assets.userBox.get(i).getPrice(), Assets.userBox.get(i).getName(), Assets.userBox.get(i).getInfo(),
																 Assets.userBox.get(i).getQuantity(), Assets.userBox.get(i).getKind(), Assets.userBox.get(i).getIndex(), xItem, yItem));
						}
						
						id = i;
						dragOKBox = false;
					}
					else
					{
						batch.draw(Assets.itemImgs[Assets.userBox.get(i).getId()], Assets.userBox.get(i).getX(), Assets.userBox.get(i).getY());
						float quantityHeight = Assets.font.getBounds("x" + Assets.userBox.get(i).getQuantity()).height;
						float quantityWidth = Assets.font.getBounds("x" + Assets.userBox.get(i).getQuantity()).width;
							
						Assets.font.draw(batch, "x" + Assets.userBox.get(i).getQuantity(), 
											Assets.userBox.get(i).getX() + ((64 - quantityWidth)/2), 
											Assets.userBox.get(i).getY() + ((64 + quantityHeight)/2));
					}
				}
			}
			
			if(i == Assets.userBox.size() - 1)
			{
				if(id != -1)
				{
					Assets.userBox.remove(id);
				}
			}
			
		}
	}
	
	
	public void dragItemBox()
	{
		//System.out.println("start method dragItemBox() in Market class");
		if(flgDragBox)
		{
			batch.begin();
			
			float x = Gdx.input.getX(0) - 32 + 128;
			float y = Gdx.graphics.getHeight() - Gdx.input.getY(0) + 64;
			
			if (Gdx.input.isTouched(0) == false)
			{
				flgDragBox = false;
				
				for(int i = 0; i < userItemPos.length; i++)
				{
					if((OverlapTester.pointInRectangle(userItemPos[i].getX(), userItemPos[i].getY(), 64, 64, x + 32, y + 32)
						&& !checkContainId(userItemPos[i].getX(), userItemPos[i].getY())) ||
						(OverlapTester.pointInRectangle(userItemPos[i].getX(), userItemPos[i].getY(), 64, 64, x + 32, y + 32)
								&& checkItemId(userBoxStt, 0)))
					{
						xItem = userItemPos[i].getX();
						yItem = userItemPos[i].getY();
						dragOKBox = true;
					}
				}
			}
			
			if(flgDragBox)
			{
				for(int i = 0; i < Assets.userBox.size(); i++)
				{
					if(Assets.userBox.get(i).getId() == userBoxStt)
					{
						batch.draw(Assets.itemImgs[Assets.userBox.get(i).getId()], x, y);
						break;
					}
				}
			}
			
			batch.end();
		}
	}
	
	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

}
